package com.portal.tag;

import java.lang.reflect.Field;
import java.util.Hashtable;

import javax.portlet.BaseURL;
import javax.portlet.PortletMode;
import javax.portlet.PortletModeException;
import javax.portlet.PortletResponse;
import javax.portlet.PortletURL;
import javax.portlet.WindowState;
import javax.portlet.WindowStateException;
import javax.servlet.jsp.JspException;

import com.portal.util.constants.TagConstants;

public abstract class PortletURLTag168 extends BaseURLTag {

  protected String portletMode = null;

  protected String windowState = null;

  protected PortletURL portletURL = null;

  @Override
  public int doStartTag() throws JspException {
    PortletResponse portletResponse = (PortletResponse) pageContext.getRequest().getAttribute(TagConstants.PORTLET_RESPONSE);

    if (portletResponse != null) {
      PortletURL portletURL = createPortletUrl(portletResponse);
      if (portletMode != null) {// set portlet mode
        try {
          PortletMode mode = (PortletMode) TEI.portletModes.get(portletMode.toUpperCase());

          if (mode == null) {
            mode = new PortletMode(portletMode);// support for custom portlet modes PLUTO-258
          }

          portletURL.setPortletMode(mode);

        } catch (PortletModeException e) {
          throw new JspException(e);
        }
      }

      if (windowState != null) {// set window state
        try {
          WindowState state = (WindowState) TEI.definedWindowStates.get(windowState.toUpperCase());

          if (state == null) {
            state = new WindowState(windowState);// support for custom window states PLUTO-258
          }

          portletURL.setWindowState(state);

        } catch (WindowStateException e) {
          throw new JspException(e);
        }
      }
      setUrl(portletURL);
    }
    return super.doStartTag();
  }

  public String getPortletMode() {
    return portletMode;
  }

  public String getWindowState() {
    return windowState;
  }

  public void setPortletMode(String portletMode) {
    this.portletMode = portletMode;
  }

  public void setWindowState(String windowState) {
    this.windowState = windowState;
  }

  @Override
  protected BaseURL getUrl() {
    return portletURL;
  }

  @Override
  protected void setUrl(BaseURL url) {
    this.portletURL = (PortletURL) url;
  }

  protected abstract PortletURL createPortletUrl(PortletResponse portletResponse);

  public static class TEI extends BaseURLTag.TEI {
    public final static Hashtable<String, Object> definedWindowStates = getDefinedWindowStates();

    public final static Hashtable<String, Object> portletModes = getDefinedPortletModes();

    private static Hashtable<String, Object> getDefinedPortletModes() {
      Hashtable<String, Object> portletModes = new Hashtable<String, Object>();
      Field[] f = PortletMode.class.getDeclaredFields();

      for (int i = 0; i < f.length; i++) {
        if (f[i].getType().isAssignableFrom(javax.portlet.PortletMode.class)) {
          try {
            portletModes.put(f[i].get(f[i]).toString().toUpperCase(), f[i].get(f[i]));
          } catch (IllegalAccessException e) {
          }
        }
      }

      return portletModes;
    }

    private static Hashtable<String, Object> getDefinedWindowStates() {
      Hashtable<String, Object> definedWindowStates = new Hashtable<String, Object>();
      Field[] f = WindowState.class.getDeclaredFields();

      for (int i = 0; i < f.length; i++) {
        if (f[i].getType().isAssignableFrom(javax.portlet.WindowState.class)) {
          try {
            definedWindowStates.put(f[i].get(f[i]).toString().toUpperCase(), f[i].get(f[i]));
          } catch (IllegalAccessException e) {

          }
        }
      }
      return definedWindowStates;
    }
  }

}
